﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using ApiBetaSeries.Factories;

namespace ApiBetaSeries.Services
{
    public class SubtitleServices : ApiBetaSeries.Services.ISubtitleServices
    {
        #region singleton
        private static SubtitleServices m_subtitleServices = null;
        private SubtitleServices() { }
        public static SubtitleServices GetInstance()
        {
            if (m_subtitleServices == null)
            {
                m_subtitleServices = new SubtitleServices();
            }
            return m_subtitleServices;
        }
        #endregion

        /// <summary>
        /// Get all the last subtitles
        /// </summary>
        /// <param name="number">number of subtitiles you want (between 0 and 100)</param>
        /// <param name="type">VO/VF or VOVF</param>
        /// <returns>List of subtitles</returns>
        [ApiBetaSeries.Version.ApiMethod(true, "subtitles/last")]
        public List<ISubtitle> GetLastSubtitles(int number = 100, SubtitleLanguage type = SubtitleLanguage.VOVF)
        {
            List<ISubtitle> list = new List<ISubtitle>();
            if (type == SubtitleLanguage.Other)
            {
                throw new ArgumentException("Cannot use SubtitleLanguage : Other for this request");
            }
            string stype = type.ToString();
            
            if (number < 0)
            {
                number = 100;
            }

            XmlDocument doc = Utils.UtilsXML.GetUrl("/subtitles/last.xml", "&number=" + number + "&language=" + stype);
            XmlElement xe = doc["root"]["subtitles"];

            if (xe == null)
            {
                throw new BetaSeriesException(1003, "Invalid action.");
            }
            foreach (XmlElement sub in xe.GetElementsByTagName("subtitle"))
            {
                ISubtitle s = SubtitleFactory.CreateSubtitle(sub);
                list.Add(s);
            }


            return list;
        }

        /// <summary>
        /// Get the last subtitle of a show
        /// </summary>
        /// <param name="urlSerie">url of the show</param>
        /// <param name="number">number of subtitles you want (between 0 and 100) </param>
        /// <param name="type">VO/VF/VOVF</param>
        /// <returns>list of subtitles</returns>
        [ApiBetaSeries.Version.ApiMethod(false, "subtitles/last")]
        public List<ISubtitle> GetLastSubtitles(string urlSerie, int number = 100, SubtitleLanguage type = SubtitleLanguage.VOVF)
        {
            IShow show = ShowFactory.CreateShow();
            show.Url = urlSerie;
            return show.GetLastSubtitles(number, type);
        }

        /// <summary>
        /// Get all the subtitles for a specific episode
        /// </summary>
        /// <param name="urlSerie">url of the show</param>
        /// <param name="season">season number of the episode</param>
        /// <param name="episode">episode number</param>
        /// <param name="type">VO/VF/VOVF</param>
        /// <returns>List of subtitles</returns>
        [ApiBetaSeries.Version.ApiMethod(false, "subtitles/show")]
        public List<ISubtitle> GetSubtitles(string urlSerie, int season, int episode, SubtitleLanguage type = SubtitleLanguage.VOVF)
        {
            IEpisode ep = EpisodeFactory.CreateEpisode();
            ep.UrlShow = urlSerie;
            ep.Season = season;
            ep.Number = episode;
            return ep.GetSubtitles(type);
        }

        /// <summary>
        /// Get all the subtitles for a specific season
        /// </summary>
        /// <param name="urlSerie">url of the show</param>
        /// <param name="season">season number of the episode</param>
        /// <param name="type">VO/VF/VOVF</param>
        /// <returns>List of subtitles</returns>
        [ApiBetaSeries.Version.ApiMethod(false, "subtitles/show")]
        public List<ISubtitle> GetSubtitles(string urlSerie, int season, SubtitleLanguage type = SubtitleLanguage.VOVF)
        {
            ISeason s = SeasonFactory.CreateSeason();
            s.UrlShow = urlSerie;
            s.Number = season;
            return s.GetSubtitles(type);
        }

        /// <summary>
        /// Get all the subtitles for a show
        /// </summary>
        /// <param name="urlSerie">url of the show</param>
        /// <param name="type">VO/VF/VOVF</param>
        /// <returns>List of subtitles</returns>
        [ApiBetaSeries.Version.ApiMethod(false, "subtitles/show")]
        public List<ISubtitle> GetSubtitles(string urlSerie, SubtitleLanguage type = SubtitleLanguage.VOVF)
        {
            IShow show = ShowFactory.CreateShow();
            show.Url = urlSerie;
            return show.GetSubtitles(type);
        }
        

    }
}
